﻿using Microsoft.EntityFrameworkCore;
using Npgsql;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.DBIBase;

namespace UtilZ.Dotnet.DBPostgreSQL
{
    internal class PostgreSQLDBInteraction : DBInteractionAbs
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public PostgreSQLDBInteraction()
            : base()
        {

        }

        private const string PARASIGN = "@";
        /// <summary>
        /// 数据库参数字符
        /// </summary>
        public override string ParaSign
        {
            get { return PARASIGN; }
        }

        /// <summary>
        /// 创建数据库拼接连接字符串
        /// </summary>
        /// <param name="config">数据库配置</param>
        /// <param name="visitType">数据库访问类型</param>
        /// <returns>数据库连接字符串</returns>
        protected override string CreateDBSpliceConStr(DatabaseConfig config, DBVisitType visitType)
        {
            //NpgsqlConnectionStringBuilder
            if (config.Port <= 0)
            {
                config.Port = 5432;
            }

            //return $@"Port={config.Port};Host={config.Host};Username={config.Account};Password={config.Password};Database={config.DatabaseName}";
            return $@"Server={config.Host};Port={config.Port};User ID={config.Account};Password={config.Password};Database={config.DatabaseName}";
        }

        /// <summary>
        /// 获取DbProviderFactory
        /// </summary>
        /// <returns>DbProviderFactory</returns>
        public override DbProviderFactory GetProviderFactory()
        {
            return NpgsqlFactory.Instance;
        }

        /// <summary>
        /// 转换原始查询SQL语句为分页查询SQL语句
        /// </summary>
        /// <param name="sqlStr">原始查询SQL语句</param>
        /// <param name="orderStr">排序字符串</param>
        /// <param name="pageIndex">目标页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="dataBaseVersion">数据库版本号信息</param>
        /// <param name="pagingAssistFieldName">分页字段名称</param>
        /// <returns>分页查询SQL语句</returns>
        protected override string ConvertSqlToPagingQuerySql(string sqlStr, string orderStr, long pageIndex, long pageSize, DataBaseVersionInfo dataBaseVersion, out string pagingAssistFieldName)
        {
            //eg:SELECT * from person WHERE ID < 100 ORDER by ID DESC limit 10 offset 0
            pagingAssistFieldName = null;
            var startIndex = (pageIndex - 1) * pageSize;
            string dstSqlStr = null;
            if (string.IsNullOrWhiteSpace(orderStr))
            {
                dstSqlStr = $@"{sqlStr} limit {pageSize} offset {startIndex}";
            }
            else
            {
                dstSqlStr = $@"{sqlStr} ORDER BY {orderStr} limit {pageSize} offset {startIndex}";
            }

            return dstSqlStr;
        }


        private readonly PostgreSQLSqlFieldValueFormator _sqlFieldValueFormator = new PostgreSQLSqlFieldValueFormator();
        /// <summary>
        /// 获取sql字段值格式化对象
        /// </summary>
        /// <returns>sql字段值格式化对象</returns>
        protected override ISqlFieldValueFormator GetSqlFieldValueFormator()
        {
            return this._sqlFieldValueFormator;
        }

        /// <summary>
        /// 设备数据库连接
        /// </summary>
        /// <param name="optionsBuilder">DbContextOptionsBuilder</param>
        /// <param name="conInfo">数据库连接</param>
        /// <returns>DbContextOptionsBuilder</returns>
        protected override DbContextOptionsBuilder PrimitiveUseDatabase(DbContextOptionsBuilder optionsBuilder, IDbConnectionInfo conInfo)
        {
            return optionsBuilder.UseNpgsql(conInfo.DbConnection);
        }
    }
}
